package ctn.batch;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import ctn.contant.MsgContant;
import ctn.contant.PlugContant;
import ctn.dao.inteface.MessageDao;
import ctn.mail.EmailSend;

@Component(value="mailTask")
@Lazy(false)
public class SendEmailTask
{
	@Autowired
	@Qualifier("filePro")
	private Properties prop;

	@Autowired
	private MessageDao messageDaoImpl;
	
	private Log log = LogFactory.getLog(SendEmailTask.class);
	
	@Scheduled(cron=" 0 1/5 * * * ? ")
    public void task()
    {
		log.info("Email start.........................");
		EmailSend es = getEmailObject();
		List<Map<String, String>> list = messageDaoImpl.getMessageQueuec();
		int count = list.size();
		log.info("There are "+count+" email to send..........");
		if (count <= 0) {
			return;
		}
		for (Map<String, String> map : list) {
			log.info("update the message status to ready..................");
			messageDaoImpl.updateMsgStatus(MsgContant.MESSAGE_STATUS_READY, 
					Integer.parseInt(map.get(MsgContant.MESSAGE_ID)));
			log.info("To mail:"+map.get(MsgContant.MESSAGE_TO_MAIL));
			es.setTomail(map.get(MsgContant.MESSAGE_TO_MAIL));
			log.info("Email subject:"+map.get(MsgContant.MESSAGE_SUBJECT));
			es.setSubject(map.get(MsgContant.MESSAGE_SUBJECT));
			log.info("Email body:"+map.get(MsgContant.MESSAGE_CONTENT));
			es.setContent(map.get(MsgContant.MESSAGE_CONTENT));
			log.info("Sending the Email...................");
			boolean flag = es.sendMail();
			if (!flag) {
				log.info("Sending the Email...................Failure");
				messageDaoImpl.updateMsgStatus(MsgContant.MESSAGE_STATUS_FAILTURE, Integer.parseInt(map.get(MsgContant.MESSAGE_ID)));
			}else{
				log.info("Sending the Email...................Success");
				messageDaoImpl.updateMsgStatus(MsgContant.MESSAGE_STATUS_SUCCESS, Integer.parseInt(map.get(MsgContant.MESSAGE_ID)));
			}
		}
		log.info("Sending the Email...................Complete");
		//these are use thread to send email
		/*if(count < 2){
			SendEmailThread setSingle = new SendEmailThread("A",messageDaoImpl,es,list.subList(0, 1));
			setSingle.start();
		}else{
			int from = 0;
			int to = count/2;
			SendEmailThread set1 = new SendEmailThread("A",messageDaoImpl,es,list.subList(from, to));
			set1.start();
			SendEmailThread set2 = new SendEmailThread("B",messageDaoImpl,es,list.subList(to, count));
			set2.start();
		}*/
		log.info("Sending the Email...................Complete");
    }
	
	/**
	 * get the email object
	 * @return
	 */
	private EmailSend getEmailObject(){
		//get the from - email info
		String host = prop.getProperty(PlugContant.E_MAIL_STAMP_HOST_KEY);
		String username = prop.getProperty(PlugContant.E_MAIL_STAMP_USER_KEY);
		String pwd = prop.getProperty(PlugContant.E_MAIL_STAMP_PWD_KEY);
		String address = prop.getProperty(PlugContant.E_MAIL_STAMP_ADDRESS_KEY);

		//initiation the email info
		EmailSend es = new EmailSend();
		es.setFromPwd(pwd);
		es.setFrommail(address);
		es.setFromUser(username);
		es.setSmtp(host);
		es.setHtmlBody(true);
		//return object
		return es;
	}
	
}